// Adjusting to Macroprudential Policies: Loan-to-value Limits and Housing Choice


//First step - merging CARMAN with mortgage loan data

clear all
set more off

**merge if obs is unique and and the match is perfect (some gush/helka)
use H:\u23y\Research\Data\data_mortgage\WorkData\Halvaot.dta,clear

*strings into date
gen new_dt_buy_neches_memuman = date(dt_buy_neches_memuman, "DMY")
format  new_dt_buy_neches_memuman %td
label variable new_dt_buy_neches_memuman "DT_BUY_NECHES_MEMUMAN"
drop  dt_buy_neches_memuman
rename new_dt_buy_neches_memuman dt_buy_neches_memuman

*drop obs if yeshuv id, asset price, buying date has missing values
drop if yeshuv_memuman_id==. | val_neches_memuman==. | dt_buy_neches_memuman==.
*check if a transaction is occuring more than once and drop if it does
egen id = group( yeshuv_memuman_id val_neches_memuman dt_buy_neches_memuman)
bysort  id: gen id_count = _N
drop if  id_count>1
drop  id id_count

save H:\u23y\Research\Data\data_mortgage\WorkData\new_halvaot.dta, replace

use H:\u23y\Research\Data\data_mortgage\WorkData\merge_karmen\karmen_raw_data_01_98_to_07_14.dta, clear

*strings into date
tostring  iska_date, replace format(%20.0f)
gen new_iska_date = date(iska_date, "YMD")
format new_iska_date %td
drop  iska_date
rename  new_iska_date iska_date

*drop obs before 2007
drop if  iska_date < date("01jan2007", "DMY")

*drop obs if yeshuv id, asset price, buying date has missing values
drop if locality_code_karmen==. | price_eval==. | iska_date==.
*check if a transaction is occuring more than once and drop if it does
egen id = group( locality_code_karmen price_eval iska_date)
bysort  id: gen id_count = _N
drop if  id_count>1
drop  id id_count

rename locality_code_karmen yeshuv_memuman_id
rename iska_date dt_buy_neches_memuman
rename price_eval val_neches_memuman

save H:\u23y\Research\Data\data_mortgage\WorkData\new_karmen_raw_data_01_98_to_07_14.dta, replace

clear
use H:\u23y\Research\Data\data_mortgage\WorkData\new_halvaot.dta
merge 1:1 yeshuv_memuman_id dt_buy_neches_memuman val_neches_memuman using "H:\u23y\Research\Data\data_mortgage\WorkData\new_karmen_raw_data_01_98_to_07_14.dta"
save H:\u22d\Nitzan\merged1.dta, replace


// check if we can use the id of the property for the merge 


**merge if obs in "Halvaot" is unique and but obs in "karmen" are not (some gush/helka)

use H:\u23y\Research\Data\data_mortgage\WorkData\merge_karmen\karmen_raw_data_01_98_to_07_14.dta, clear

*strings into date
tostring  iska_date, replace format(%20.0f)
gen new_iska_date = date(iska_date, "YMD")
format new_iska_date %td
drop  iska_date
rename  new_iska_date iska_date

*drop obs before 2007
drop if  iska_date < date("01jan2007", "DMY")

*drop obs if yeshuv id, asset price, buying date has missing values
drop if locality_code_karmen==. | price_eval==. | iska_date==.

rename locality_code_karmen yeshuv_memuman_id
rename iska_date dt_buy_neches_memuman
rename price_eval val_neches_memuman

save H:\u23y\Research\Data\data_mortgage\WorkData\Karmenfewobs, replace

clear
use H:\u23y\Research\Data\data_mortgage\WorkData\new_halvaot.dta
merge 1:m yeshuv_memuman_id dt_buy_neches_memuman val_neches_memuman using "H:\u23y\Research\Data\data_mortgage\WorkData\Karmenfewobs"
egen id = group( yeshuv_memuman_id val_neches_memuman dt_buy_neches_memuman)
bysort  id: gen id_count = _N

*isolate gush number
split gush_helka_neches_memuman, p( "-" ";" "/" "\" )
gen gush_halvaot = substr( gush_helka_neches_memuman1, 1, 3)
replace gush_halvaot = substr( gush_helka_neches_memuman2, 1, 3) if gush_helka_neches_memuman1==""
replace gush_halvaot = substr( gush_helka_neches_memuman3, 1, 3) if gush_helka_neches_memuman1=="" & gush_helka_neches_memuman2==""
replace gush_halvaot = substr( gush_helka_neches_memuman4, 1, 3) if gush_helka_neches_memuman1=="" & gush_helka_neches_memuman2=="" ///
		& gush_helka_neches_memuman3==""
gen dum1 = ( gush_halvaot=="" &  gush_helka_neches_memuman!="")
tab dum1
drop dum1
tostring gush_helka, generate(gush_helka1) format(%17.0g)
gen gush_karmen = substr(gush_helka1, 1, 3)
gen dum1 = ( gush_halvaot ==  gush_karmen )
drop gush_helka_neches_memuman1 gush_helka_neches_memuman2 gush_helka_neches_memuman3 gush_helka_neches_memuman4 gush_helka_neches_memuman5 ///
	gush_helka_neches_memuman6 gush_helka_neches_memuman7 gush_helka_neches_memuman8  gush_helka1
gen dum2 = ( gush_halvaot ==  gush_karmen ) if _merge==3 & id_count>1 //dummt variable==1 if first 3 digits of gush/helka are equal and merge was //
//successfull and there is more than one match

save H:\u23y\Research\Data\data_mortgage\WorkData\merged2.dta, replace

// using the date variable for the merge between CARMAN and Mortgage Loan Dataset


clear all
set more off
**merge by yeshuv_memuman_id, val_neches_memuman

use H:\u23y\Research\Data\data_mortgage\WorkData\Halvaot.dta,clear

*strings into date
gen new_dt_buy_neches_memuman = date(dt_buy_neches_memuman, "DMY")
format new_dt_buy_neches_memuman %td
label variable new_dt_buy_neches_memuman "DT_BUY_NECHES_MEMUMAN"
drop dt_buy_neches_memuman
rename new_dt_buy_neches_memuman dt_buy_neches_memuman

*drop obs if yeshuv id, asset price has missing values
drop if yeshuv_memuman_id==. | val_neches_memuman==. 
*isolate gush number
split gush_helka_neches_memuman, p( "-" ";" "/" "\" )
gen gush_halvaot = substr( gush_helka_neches_memuman1, 1, 3)
replace gush_halvaot = substr( gush_helka_neches_memuman2, 1, 3) if gush_helka_neches_memuman1==""
replace gush_halvaot = substr( gush_helka_neches_memuman3, 1, 3) if gush_helka_neches_memuman1=="" & gush_helka_neches_memuman2==""
replace gush_halvaot = substr( gush_helka_neches_memuman4, 1, 3) if gush_helka_neches_memuman1=="" & gush_helka_neches_memuman2=="" ///
		& gush_helka_neches_memuman3==""
gen dum1 = ( gush_halvaot=="" &  gush_helka_neches_memuman!="")
tab dum1
drop dum1 gush_helka_neches_memuman1 gush_helka_neches_memuman2 gush_helka_neches_memuman3 gush_helka_neches_memuman4 gush_helka_neches_memuman5 ///
	gush_helka_neches_memuman6 gush_helka_neches_memuman7 gush_helka_neches_memuman8

save H:\u23y\Research\Data\data_mortgage\WorkData\merge_karmen\new_halvaot.dta, replace

use H:\u23y\Research\Data\data_mortgage\WorkData\merge_karmen\karmen_raw_data_01_98_to_07_14.dta, clear

*strings into date
tostring iska_date, replace format(%20.0f)
gen new_iska_date = date(iska_date, "YMD")
format new_iska_date %td
drop iska_date
rename new_iska_date iska_date

*drop obs before 2007
drop if  iska_date < date("01jan2007", "DMY")

*drop obs if yeshuv id, asset price has missing values
drop if locality_code_karmen==. | price_eval==.

rename locality_code_karmen yeshuv_memuman_id
rename price_eval val_neches_memuman

tostring gush_helka, generate(gush_helka1) format(%17.0g)
gen gush_karmen = substr(gush_helka1, 1, 3)

save H:\u23y\Research\Data\data_mortgage\WorkData\merge_karmen\new_karmen_raw_data_01_98_to_07_14.dta, replace

clear
use H:\u23y\Research\Data\data_mortgage\WorkData\merge_karmen\new_halvaot.dta
merge m:m yeshuv_memuman_id val_neches_memuman using "H:\u23y\Research\Data\data_mortgage\WorkData\merge_karmen\new_karmen_raw_data_01_98_to_07_14.dta"

drop if _merge!=3

save H:\u23y\Research\Data\data_mortgage\WorkData\merge_karmen\mergedfordate.dta, replace

*no date and match is unique
egen id_without_gush = group( yeshuv_memuman_id val_neches_memuman )
bysort id_without_gush: gen id_without_gush_count = _N
drop if dt_buy_neches_memuman!=.
drop if id_without_gush_count>1

save H:\u23y\Research\Data\data_mortgage\WorkData\merge_karmen\merged3.dta, replace 



//Second step - data processing (the original file includes 405781 obs.) 

//soft LTV limit

 
use U:\PHD\halvaot.dta
gen date=date( dt_bizua_halvaa,"DMY")
format date %td
gen year=year(date)
gen month=month(date)
drop if year<2010
drop if shiur_ahuz_heczer_from_incom==0
drop if shiur_ahuz_heczer_from_incom>100
drop if price> 50000000
drop if   yeshuv_memuman_id== .
drop if   yeshuv_memuman_id== 9999
drop if lovim_num>=3
drop if  purpose_halvaa_id>=5
drop if year<2010
count if  sum_incom<2000| sum_incom>100000
count if  val_neches_memuman<100000|val_neches_memuman>5000000
count if  sum_halvaa_bizua<50000| sum_halvaa_bizua >2500000
count if ltv==0


gen today= c(current_date)
gen todayAge=date(today,"DMY")
format  todayAge  %td
gen dateOdborn=date( dt_born,"DMY")
format dateOdborn %td
gen age = (   todayAge- dob) / 365.25 
egen aveAge=mean(age), by(loan_num)
keep if aveAge >20& aveAge <80
gen after=(date>td(26,10,2010))
drop if  date>td(1,5,2011)
destring  central, gen (merkaz)
drop if  sum_incom<2000| sum_incom>100000
drop if  price_eval<100000|price_eval>5000000
drop if  sum_halvaa_bizua<50000| sum_halvaa_bizua >2500000

//  639 obs. dropped before the year 2010, shiur_ahuz_heczer_from_incom = mortgage payment to income ratio, price = price of the housing unit, yeshuv_memuman_id - the location (city) of the proporty. we drop loans with more>2 borrowers.  purpose_halvaa_id- the purpose of the loan (>5 means that it is a consumer loan). dt_bizua_halvaa- the date of the loan. aveAge and income are the average age and income per household. we now have 309061 obs. 

sum price sum_incom aveAge ltv interestrate  aveTkufa area_net rooms dist_from_tlv socio_eco_level_08_cbs
sum price sum_incom aveAge ltv interestrate  aveTkufa area_net rooms dist_from_tlv socio_eco_level_08_cbs if group==1

sum price sum_incom aveAge ltv interestrate  aveTkufa area_net rooms dist_from_tlv socio_eco_level_08_cbs if group==2
sum price sum_incom aveAge ltv interestrate  aveTkufa area_net rooms dist_from_tlv socio_eco_level_08_cbs if group==3



//Generating distance from Tel Aviv measure
do "H:\u23y\Research\Data\data_mortgage\WorkData\merge_karmen\nnmatch.ado"
gen x_cord = floor(coordinate_2011 / 100000)
gen y_cord = mod(coordinate_2011, 100000)
local tlv_x = 18055
local tlv_y = 66574
gen dist_from_tlv = ((x_cord - `tlv_x') ^ 2 + (y_cord - `tlv_y') ^ 2) ^ 0.5
replace dist_from_tlv  = dist_from_tlv /100


// generating groups First-time Home Buyers - Upgraders - Investors . buyer_cause==1 (FTHB), buyer_cause==2+3 Upgraders, Investors ==4
gen group =1 if buyer_cause==1
replace group=2 if buyer_cause==2|buyer_cause==3
replace group=3 if buyer_cause==4
drop if  buyer_cause==5
drop if  buyer_cause==0
drop if  buyer_cause==6
drop if  buyer_cause==7

count if after==0
count if after==1
xtile  income= sum_incom, n(10)
xtile agextile=  aveAge, n(10)

hist income
hist agextile

// basic figures for LTV distributionm for diff borrowers types
twoway kdensity ltv, by(group)
kdensity ltv if buyer_cause==1&after==1, addplot(kdensity ltv if buyer_cause==1&after==0) legend(ring(0) pos(2) label(1 "After") label(2 "Before") )
kdensity ltv if group==2&after==1, addplot(kdensity ltv if group==2&after==0) legend(ring(0) pos(2) label(1 "After") label(2 "Before"))
kdensity ltv if group==3&after==1, addplot(kdensity ltv if group==3&after==0) legend(ring(0) pos(2) label(1 "After") label(2 "Before"))

twoway (kdensity ltv if group==1) || (kdensity ltv if group==2) || (kdensity ltv if group==3)
twoway (kdensity ltv if group==1&after==0) || (kdensity ltv if group==2&after==0) || (kdensity ltv if group==3&after==0)
twoway (kdensity ltv if group==1&after==1) || (kdensity ltv if group==2&after==1) || (kdensity ltv if group==3&after==1)
hist ltv, by(group)
g box = "0-10" if ltv < 10
replace box = "10-20" if ltv >= 10 & ltv < 20
replace box = "20-30" if ltv >= 20 & ltv < 30
replace box = "30-40" if ltv >= 30 & ltv < 40
replace box = "40-50" if ltv >= 40 & ltv < 50
replace box = "50-60" if ltv >= 50 & ltv < 60
replace box = "60-70" if ltv >= 60 & ltv < 70
replace box = "70-80" if ltv >= 70 & ltv < 80
replace box = "80-90" if ltv >= 80 & ltv < 90
replace box = "90-100" if ltv >90

g box = 1 if ltv <5
replace box = 2 if ltv >=5 & ltv < 10
replace box = 3 if ltv >= 10 & ltv < 15
replace box = 4 if ltv >= 15& ltv < 20
replace box = 5 if ltv >= 20 & ltv < 25
replace box = 6 if ltv >= 25 & ltv < 30
replace box = 7 if ltv >= 30 & ltv < 40
replace box = 8 if ltv >= 35 & ltv < 40
replace box = 9 if ltv >= 40 & ltv < 45
replace box = 10 if ltv >=  45 & ltv < 50
replace box = 11 if ltv >= 50 & ltv < 55
replace box = 12 if ltv >= 55 & ltv < 60
replace box = 13 if ltv >= 60 & ltv < 65
replace box = 14 if ltv >= 65 & ltv < 70
replace box = 15 if ltv >= 70 & ltv < 75
replace box = 16 if ltv >= 75 & ltv < 80
replace box = 17 if ltv >= 80 & ltv < 85
replace box = 18 if ltv >= 85 & ltv < 90
replace box = 19 if ltv >= 90 & ltv < 95
replace box = 20 if ltv >95

graph bar (percent) ltv group==3,over(after,gap(10)) over (box ) asyvars bar(2,color(blue)) bar(1,color(red))
graph bar (percent) ltv if after==0,over(group,gap(10)) over (box ) asyvars bar(2,color(blue)) bar(1,color(black)) bar(3,color(green))
graph bar (percent) ltv if after==1,over(group,gap(10)) over (box ) asyvars bar(2,color(blue)) bar(1,color(black)) bar(3,color(green))
graph bar (percent) ltv if group==1,over(after,gap(10)) over (box ) asyvars bar(2,color(blue)) bar(1,color(black))
graph bar (percent) ltv if group==2,over(after,gap(10)) over (box ) asyvars bar(2,color(blue)) bar(1,color(black))
graph bar (percent) ltv if group==3,over(after,gap(10)) over (box ) asyvars bar(2,color(blue)) bar(1,color(black))
twoway (kdensity  socio_eco_level_08_cbs if group==1) || (kdensity socio_eco_level_08_cbs if group==2) || (kdensity socio_eco_level_08_cbs if group==3)
kdensity  area_net if group==1&after==0, addplot(kdensity area_net if group==1&after==1) legend(ring(0) pos(2) label(1 "before") label(2 "after"))
twoway (kdensity area_net if area_net  <200&group==1) || (kdensity area_net  if area_net  <200& group==2) || (kdensity area_net if area_net  <200&group==3)

gen leverage=1 if ltv<75
replace leverage=2 if ltv>75
kdensity  sum_incom if after==0&ltv<75, addplot(kdensity sum_incom if after==0&ltv>=75) legend(ring(0) pos(2) label(1 "LTV<75%") label(2 "LTV>=75 %"))

// age distribution

kdensity  aveAge if year == 2010, addplot(kdensity aveAge if year == 2011 || kdensity aveAge if year == 2012) legend(ring(0) pos(2) label(1 "20> 10") label(2 "2011") label(3 "2012"))
gen age_c=1
replace age_c=2 if aveAge >35& aveAge <50
replace age_c=3 if aveAge >=50
tab age_c
tab age_c if after==1
tab age_c if after==0

kdensity  aveAge if after==0 &  merkaz==1, addplot(kdensity aveAge if after==1 & merkaz==1) legend(ring(0) pos(2) label(1 "before") label (2 "after"))

// income distribution
gen logSalary= log( salary_month_net)

kdensity  logSalary  if year == 2010 , addplot(kdensity logSalary if year == 2011  || kdensity  logSalary  if year == 2012) legend(ring(0) pos) label(1 "2010") label(2 "2011") label(3 "2012"))
ksmirnov logSalary, by (after)

// House price dist

en lnprice = log( val_neches_memuman)
kdensity  lnprice if year == 2010 , addplot(kdensity lnprice if year == 2011  || kdensity  ltv  if lnprice == 2012) legend(ring(0) pos(2) label(1 "2010") label(2 "2011") label(3 "2012"))



// calculating duration of the loan as an average of the different loans

gen weights100=weights/100
gen sumtkufa=   weights100* tkufa_mekorit_peron_mnts
egen aveTkufa= sum(sumtkufa), by(loan_num)

duplicates drop  loan_num, force

kdensity  aveTkufa if after==0, addplot(kdensity  aveTkufa if after==1) legend(ring(0) pos(2) label(1 "before") label(2 "after"))
ksmirnov aveTkufa, by (after)
gen tkufayear= aveTkufa/12

. kdensity  tkufayear if after==0, addplot(kdensity  tkufayear if after==1) legend(ring(0) pos(2) label(1 "before") label(2 "after"))



// matching 



nnmatch  dist_from_tlv after  sum_incom  aveAge , tc(ate) bias(bias) keep(H:\u23y\Research\Data\data_mortgage\WorkData\matching\dist_from_tlv.dta) replace
nnmatch  rooms after  sum_incom  aveAge , tc(ate) bias(bias) keep(H:\u23y\Research\Data\data_mortgage\WorkData\matching\rooms.dta) replace
nnmatch  area_net after  sum_incom  aveAge , tc(ate) bias(bias) keep(H:\u23y\Research\Data\data_mortgage\WorkData\matching\area_net.dta) replace
nnmatch  price after  sum_incom  aveAge , tc(ate) bias(bias) keep(H:\u23y\Research\Data\data_mortgage\WorkData\matching\price.dta) replace
nnmatch  val_neches_memuman after  sum_incom  aveAge , tc(ate) bias(bias) keep(H:\u23y\Research\Data\data_mortgage\WorkData\matching\val_neches_memuman.dta) replace
nnmatch  socio_eco_level_08_cbs after  sum_incom  aveAge , tc(ate) bias(bias) keep(H:\u23y\Research\Data\data_mortgage\WorkData\matching\socio_eco_level_08_cbs.dta) replace


nnmatch  dist_from_tlv after  sum_incom  aveAge , tc(att) bias(bias) keep(H:\u23y\Research\Data\data_mortgage\WorkData\matching\dist_from_tlv.dta) replace
nnmatch  rooms after  sum_incom  aveAge , tc(att) bias(bias) keep(H:\u23y\Research\Data\data_mortgage\WorkData\matching\rooms.dta) replace
nnmatch  area_net after  sum_incom  aveAge , tc(att) bias(bias) keep(H:\u23y\Research\Data\data_mortgage\WorkData\matching\area_net.dta) replace
nnmatch  price after  sum_incom  aveAge , tc(att) bias(bias) keep(H:\u23y\Research\Data\data_mortgage\WorkData\matching\price.dta) replace
nnmatch  val_neches_memuman after  sum_incom  aveAge , tc(att) bias(bias) keep(H:\u23y\Research\Data\data_mortgage\WorkData\matching\val_neches_memuman.dta) replace
nnmatch  socio_eco_level_08_cbs after  sum_incom  aveAge , tc(att) bias(bias) keep(H:\u23y\Research\Data\data_mortgage\WorkData\matching\socio_eco_level_08_cbs.dta) replace

//matching by age and by buyer_cause


nnmatch  dist_from_tlv after  sum_incom  aveAge if aveAge<40, tc(ate) bias(bias)    
nnmatch  rooms after  sum_incom  aveAge if aveAge<40, tc(ate) bias(bias)  
nnmatch  area_net after  sum_incom  aveAge if aveAge<40, tc(ate) bias(bias) 
nnmatch  price after  sum_incom  aveAge if aveAge<40, tc(ate) bias(bias)  
nnmatch  val_neches_memuman after  sum_incom  aveAge if aveAge<40, tc(ate) 
nnmatch  socio_eco_level_08_cbs after  sum_incom  aveAge if aveAge<40, tc(ate) bias(bias) 

nnmatch  dist_from_tlv after  sum_incom  aveAge if aveAge>=40, tc(ate) bias(bias)  
nnmatch  rooms after  sum_incom  aveAge if aveAge>=40, tc(ate) bias(bias)  
nnmatch  area_net after  sum_incom  aveAge if aveAge>=40, tc(ate) bias(bias)  
nnmatch  price after  sum_incom  aveAge if aveAge>=40, tc(ate) bias(bias)  
nnmatch  val_neches_memuman after  sum_incom  aveAge if aveAge>=40, tc(ate) bias(bias) 
nnmatch  socio_eco_level_08_cbs after  sum_incom  aveAge if aveAge>=40, tc(ate) bias(bias) 

nnmatch  dist_from_tlv after  sum_incom  aveAge if group==1 tc(ate) bias(bias)    
nnmatch  rooms after  sum_incom  aveAge if group==1, tc(ate) bias(bias)  
nnmatch  area_net after  sum_incom  aveAge if group==1, tc(ate) bias(bias) 
nnmatch  price after  sum_incom  aveAge if group==1, tc(ate) bias(bias)  
nnmatch  val_neches_memuman after  sum_incom  aveAge if group==1, tc(ate) 
nnmatch  socio_eco_level_08_cbs after  sum_incom  aveAge if group==1, tc(ate) bias(bias) 


nnmatch  dist_from_tlv after  sum_incom  aveAge if group==2, tc(ate) bias(bias)  
nnmatch  rooms after  sum_incom  aveAge if group==2, tc(ate) bias(bias)  
nnmatch  area_net after  sum_incom  aveAge if group==2, tc(ate) bias(bias)  
nnmatch  price after  sum_incom  aveAge if group==2, tc(ate) bias(bias)  
nnmatch  val_neches_memuman after  sum_incom  aveAge if group==2, tc(ate) bias(bias) 
nnmatch  socio_eco_level_08_cbs after  sum_incom  aveAge if group==2, tc(ate) bias(bias) 


nnmatch  dist_from_tlv after  sum_incom  aveAge if group==3, tc(ate) bias(bias)  
nnmatch  rooms after  sum_incom  aveAge if group==3, tc(ate) bias(bias)  
nnmatch  area_net after  sum_incom  aveAge if group==3, tc(ate) bias(bias)  
nnmatch  price after  sum_incom  aveAge if group==3, tc(ate) bias(bias)  
nnmatch  val_neches_memuman after  sum_incom  aveAge if group==3, tc(ate) bias(bias) 
nnmatch  socio_eco_level_08_cbs after  sum_incom  aveAge if group==3, tc(ate) bias(bias) 
 
 
nnmatch  dist_from_tlv after  sum_incom  aveAge if aveAge<40, tc(att) bias(bias)    
nnmatch  rooms after  sum_incom  aveAge if aveAge<40, tc(att) bias(bias)  
nnmatch  area_net after  sum_incom  aveAge if aveAge<40, tc(att) bias(bias) 
nnmatch  price after  sum_incom  aveAge if aveAge<40, tc(att) bias(bias)  
nnmatch  val_neches_memuman after  sum_incom  aveAge if aveAge<40, tc(att) 
nnmatch  socio_eco_level_08_cbs after  sum_incom  aveAge if aveAge<40, tc(att) bias(bias) 

nnmatch  dist_from_tlv after  sum_incom  aveAge if aveAge>=40, tc(att) bias(bias)  
nnmatch  rooms after  sum_incom  aveAge if aveAge>=40, tc(att) bias(bias)  
nnmatch  area_net after  sum_incom  aveAge if aveAge>=40, tc(att) bias(bias)  
nnmatch  price after  sum_incom  aveAge if aveAge>=40, tc(att) bias(bias)  
nnmatch  val_neches_memuman after  sum_incom  aveAge if aveAge>=40, tc(att) bias(bias) 
nnmatch  socio_eco_level_08_cbs after  sum_incom  aveAge if aveAge>=40, tc(att) bias(bias) 


 
// Center ("merkaz"")-Periphery

nnmatch  dist_from_tlv after  sum_incom  aveAge if merkaz==1, tc(ate) bias(bias)  
nnmatch  rooms after  sum_incom  aveAge if merkaz==1, tc(ate) bias(bias)  
nnmatch  area_net after  sum_incom  aveAge if merkaz==1, tc(ate) bias(bias)  
nnmatch  price after  sum_incom  aveAge if merkaz==1, tc(ate) bias(bias)  
nnmatch  val_neches_memuman after  sum_incom  aveAge if merkaz==1, tc(ate) bias(bias) 
nnmatch  socio_eco_level_08_cbs after  sum_incom  aveAge if merkaz==1, tc(ate) bias(bias) 


nnmatch  dist_from_tlv after  sum_incom  aveAge if merkaz==0, tc(ate) bias(bias)  
nnmatch  rooms after  sum_incom  aveAge if merkaz==0, tc(ate) bias(bias)  
nnmatch  area_net after  sum_incom  aveAge if merkaz==0, tc(ate) bias(bias)  
nnmatch  price after  sum_incom  aveAge if merkaz==0, tc(ate) bias(bias)  
nnmatch  val_neches_memuman after  sum_incom  aveAge if merkaz==0, tc(ate) bias(bias) 
nnmatch  socio_eco_level_08_cbs after  sum_incom  aveAge if merkaz==0, tc(ate) bias(bias) 

** Figure 1- used data from the paper "The effect of LTV-based risk weights on house prices: Evidence from an Israeli macroprudential policy" W. Steven Laufer

**figure 2

 kdensity  ltv if after==0&sum_halvaa_meushar>=600000&sum_halvaa_meushar<=700000, addplot(kdensity  ltv if after==1&sum_halvaa_meushar>=600000&sum_halvaa_meushar<=700000) legend(ring(0) pos(2) label(1 "Before") label(2 "After"))

 
 kdensity  ltv if after==0&sum_halvaa_meushar>=900000&sum_halvaa_meushar<=1000000, addplot(kdensity  ltv if after==1&sum_halvaa_meushar>=900000&sum_halvaa_meushar<=1000000) legend(ring(0) pos(2) label(1 "Before") label(2 "After"))

 
 gen treatment=1 if ltv==61
 replace treatment=0 if ltv==59
 
 reg interestrate treatment##after sum_incom aveAge  bank aveTkufa
  reg interestrate treatment##after sum_incom aveAge  bank 
reg Spread treatment##after sum_incom aveAge  bank aveTkufa
  reg Spread treatment##after sum_incom aveAge  bank 

 gen treatment1=1 if ltv>=61&ltv<=65
 replace treatment1=0 if ltv>=55&ltv<=59
 
 reg interestrate treatment1##after sum_incom aveAge  bank aveTkufa
 reg interestrate treatment1##after sum_incom aveAge  bank 
reg Spread treatment1##after sum_incom aveAge  bank aveTkufa
  reg Spread treatment1##after sum_incom aveAge  bank 

  
  
  gen treated=1 if sum_halvaa_meushar>=850000&sum_halvaa_meushar<=9500000
 replace treated=0 if sum_halvaa_meushar>=650000&sum_halvaa_meushar<=750000
 
  reg price treated##after aveAge sum_incom
  reg rooms treated##after aveAge sum_incom
  reg area_net treated##after aveAge sum_incom
  reg dist_from_tlv treated##after aveAge sum_incom
  reg socio_eco_level_08_cbs treated##after aveAge sum_incom
  
   teffects nnmatch price treated##after aveAge sum_incom, vce{iid}
  teffects nnmatch rooms treated##after aveAge sum_incom, vce{iid}
  teffects nnmatch area_net treated##after aveAge sum_incom, vce{iid}
  teffects nnmatch dist_from_tlv treated##after aveAge sum_incom, vce{iid}
  teffects nnmatch socio_eco_level_08_cbs treated##after aveAge sum_incom, vce{iid}

drop treated
  
  gen treated=1 if sum_halvaa_meushar>=800000&sum_halvaa_meushar<=9000000
 replace treated=0 if sum_halvaa_meushar>=700000&sum_halvaa_meushar<=800000
 
  reg price treated##after aveAge sum_incom
  reg rooms treated##after aveAge sum_incom
  reg area_net treated##after aveAge sum_incom
  reg dist_from_tlv treated##after aveAge sum_incom
  reg socio_eco_level_08_cbs treated##after aveAge sum_incom
  
  
  teffects nnmatch price treated##after aveAge sum_incom, vce{iid}
  teffects nnmatch rooms treated##after aveAge sum_incom, vce{iid}
  teffects nnmatch area_net treated##after aveAge sum_incom, vce{iid}
  teffects nnmatch dist_from_tlv treated##after aveAge sum_incom, vce{iid}
  teffects nnmatch socio_eco_level_08_cbs treated##after aveAge sum_incom, vce{iid}
  
drop treated
 gen treated=1 if sum_halvaa_meushar>=900000&sum_halvaa_meushar<=1000000
 replace treated=0 if sum_halvaa_meushar>=600000&sum_halvaa_meushar<=700000
 
  reg price treated##after aveAge sum_incom
  reg rooms treated##after aveAge sum_incom
  reg area_net treated##after aveAge sum_incom
  reg dist_from_tlv treated##after aveAge sum_incom
  reg socio_eco_level_08_cbs treated##after aveAge sum_incom
  
    teffects nnmatch price treated##after aveAge sum_incom, vce{iid}
    teffects nnmatch rooms treated##after aveAge sum_incom, vce{iid}
    teffects nnmatch area_net treated##after aveAge sum_incom, vce{iid}
    teffects nnmatch dist_from_tlv treated##after aveAge sum_incom, vce{iid}
    teffects nnmatch socio_eco_level_08_cbs treated##after aveAge sum_incom, vce{iid}

  
  
drop treated
 
**figure 3

 kdensity  interestrate if after==0&ltv>60, addplot(kdensity  interestrate if after==1&ltv>60) legend(ring(0) pos(2) label(1 "Before") label(2 "After"))

**figure 4

 kdensity  ltv if after==0, addplot(kdensity  ltv if after==1) legend(ring(0) pos(2) label(1 "Before") label(2 "After"))

 


// Strict LTV limit


use U:\work_data\merged_carmen.dta
  keep if _merge==3
rename _merge _mer
  merge m:m  bnk_kod loan_num using "U:\work_data\datagood.dta"
   duplicates drop  loan_num, force
    do "U:\tryNO1\nmatch.do"
drop if  val_neches_memuman==.
gen date=date( dt_bizua_halvaa,"DMY")
 format date %td
 gen after=(date>td(1,11,2012))
drop if date<td(25,10,2010)
drop if date>td(1,10,2013)
 drop if month==11&year==2012
drop if month==12&year==2012
count if after==0
count if after==1

**  we have total of 34k obs, with 16980 obs after and 17552 before. buyer_cause ==1 -FTHB, 2+3- UPGRADERS, 4- INVESTORS
gen group =1 if buyer_cause==1
replace group=2 if buyer_cause==2|buyer_cause==3
replace group=3 if buyer_cause==4
drop if  buyer_cause==5
drop if  buyer_cause==0
drop if  buyer_cause==6
drop if  buyer_cause==7
count if  sum_incom<2000| sum_incom>100000
count if  val_neches_memuman<100000|val_neches_memuman>5000000
count if  sum_halvaa_bizua<50000| sum_halvaa_bizua >2500000
count if ltv==0
drop if shiur_ahuz_heczer_from_incom>100
drop if shiur_ahuz_heczer_from_incom<0
drop if ltv<=0

sum price sum_incom aveAge ltv interestrate  aveTkufa area_net rooms dist_from_tlv socio_eco_level_08_cbs
sum price sum_incom aveAge ltv interestrate  aveTkufa area_net rooms dist_from_tlv socio_eco_level_08_cbs if group==1

sum price sum_incom aveAge ltv interestrate  aveTkufa area_net rooms dist_from_tlv socio_eco_level_08_cbs if group==2
sum price sum_incom aveAge ltv interestrate  aveTkufa area_net rooms dist_from_tlv socio_eco_level_08_cbs if group==3

nnmatch val_neches_memuman  after  sum_incom  aveAge, tc(att)  m(1) biasadj(bias) keep(H:\u23y\Research\Data\data_mortgage\WorkData\matching\REG2_FOB_attprice.dta) replace

twoway kdensity ltv, by(group)
 kdensity  ltv if after==0&group==3, addplot(kdensity ltv if after==1&group==3) legend(ring(0) pos(2) label(1 "Before") label(2 "After"))
 kdensity  ltv if after==0&group==2, addplot(kdensity ltv if after==1&group==3) legend(ring(0) pos(2) label(1 "Before") label(2 "After"))
 kdensity  ltv if after==0&group==1, addplot(kdensity ltv if after==1&group==3) legend(ring(0) pos(2) label(1 "Before") label(2 "After"))

 kdensity  ltv if after==0, addplot(kdensity ltv if after==1) legend(ring(0) pos(2) label(1 "Before") label(2 "After"))
 twoway (kdensity ltv if group==1) || (kdensity ltv if group==2) || (kdensity ltv if group==3)
 twoway (kdensity ltv if group==1&after==0) || (kdensity ltv if group==2&after==0) || (kdensity ltv if group==3&after==0)
 twoway (kdensity ltv if group==1&after==1) || (kdensity ltv if group==2&after==1) || (kdensity ltv if group==3&after==1)
 hist ltv, by(group)
 graph bar (percent) ltv group==3,over(after,gap(10)) over (box ) asyvars bar(2,color(blue)) bar(1,color(red))
 graph bar (percent) ltv if after==0,over(group,gap(10)) over (box ) asyvars bar(2,color(blue)) bar(1,color(black)) bar(3,color(green))
 graph bar (percent) ltv if after==1,over(group,gap(10)) over (box ) asyvars bar(2,color(blue)) bar(1,color(black)) bar(3,color(green))
 graph bar (percent) ltv if group==1,over(after,gap(10)) over (box ) asyvars bar(2,color(blue)) bar(1,color(black))
 graph bar (percent) ltv if group==2,over(after,gap(10)) over (box ) asyvars bar(2,color(blue)) bar(1,color(black))
 graph bar (percent) ltv if group==3,over(after,gap(10)) over (box ) asyvars bar(2,color(blue)) bar(1,color(black))
 twoway (kdensity  socio_eco_level_08_cbs if group==1) || (kdensity socio_eco_level_08_cbs if group==2) || (kdensity socio_eco_level_08_cbs if group==3)
 kdensity  area_net if group==1&after==0, addplot(kdensity area_net if group==1&after==0) legend(ring(0) pos(2) label(1 "before") label(2 "after"))
 twoway (kdensity area_net if area_net  <200&group==1) || (kdensity area_net  if area_net  <200& group==2) || (kdensity area_net if area_net  <200&group==3)


g box = 1 if ltv <=5
replace box = 2 if ltv >5 & ltv <= 10
replace box = 3 if ltv > 10 & ltv <= 15
replace box = 4 if ltv > 15& ltv <= 20
replace box = 5 if ltv > 20 & ltv <= 25
replace box = 6 if ltv > 25 & ltv <= 30
replace box = 7 if ltv > 30 & ltv <= 40
replace box = 8 if ltv > 35 & ltv <= 40
replace box = 9 if ltv > 40 & ltv <= 45
replace box = 10 if ltv >  45 & ltv <= 50
replace box = 11 if ltv > 50 & ltv <= 55
replace box = 12 if ltv > 55 & ltv <= 60
replace box = 13 if ltv > 60 & ltv <= 65
replace box = 14 if ltv > 65 & ltv <= 70
replace box = 15 if ltv > 70 & ltv <= 75
replace box = 16 if ltv > 75 & ltv <= 80
replace box = 17 if ltv > 80 & ltv <= 85
replace box = 18 if ltv > 85 & ltv <= 90
replace box = 19 if ltv > 90 & ltv <= 95
replace box = 20 if ltv >95
graph bar (percent) ltv if group==3,over(after,gap(10)) over (box ) asyvars bar(1,color(blue)) bar(2,color(red))
xtile  income= sum_incom, n(10)
xtile agextile=  aveAge, n(10)



g box = 1 if ltv <=1
replace box = 2 if ltv >1 & ltv <= 2
replace box = 3 if ltv > 2 & ltv <= 3
replace box = 4 if ltv > 3& ltv <= 4
replace box = 5 if ltv > 4 & ltv <= 5
replace box = 6 if ltv > 5 & ltv <= 6
replace box = 7 if ltv > 6 & ltv <= 7
replace box = 8 if ltv > 7 & ltv <= 8
replace box = 9 if ltv > 8 & ltv <= 9
replace box = 10 if ltv >  9 & ltv <= 10
replace box = 11 if ltv > 10 & ltv <= 11
replace box = 12 if ltv > 11 & ltv <= 12
replace box = 13 if ltv > 12 & ltv <= 13
replace box = 14 if ltv > 13 & ltv <= 14
replace box = 15 if ltv > 14 & ltv <= 15
replace box = 16 if ltv > 15 & ltv <= 16
replace box = 17 if ltv > 16 & ltv <= 17
replace box = 18 if ltv > 17 & ltv <= 18
replace box = 19 if ltv > 18 & ltv <= 19
replace box = 20 if ltv > 19 & ltv <= 20
replace box = 21 if ltv > 20 & ltv <= 21
replace box = 22 if ltv > 21 & ltv <= 22
replace box = 23 if ltv > 22 & ltv <= 23
replace box = 24 if ltv > 23 & ltv <= 24
replace box = 25 if ltv > 24 & ltv <= 25
replace box = 26 if ltv > 25 & ltv <= 26
replace box = 27 if ltv > 26 & ltv <= 27
replace box = 28 if ltv > 27 & ltv <= 28
replace box = 29 if ltv > 28 & ltv <= 29
replace box = 30 if ltv > 29 & ltv <= 30
replace box = 31 if ltv > 30 & ltv <= 31
replace box = 32 if ltv > 31 & ltv <= 32
replace box = 33 if ltv > 32 & ltv <= 33
replace box = 34 if ltv > 33 & ltv <= 34
replace box = 35 if ltv > 34 & ltv <= 35
replace box = 36 if ltv > 35 & ltv <= 36
replace box = 37 if ltv > 36 & ltv <= 37
replace box = 38 if ltv > 37 & ltv <= 38
replace box = 39 if ltv > 38 & ltv <= 39
replace box = 40 if ltv > 39 & ltv <= 40
replace box = 41 if ltv > 40 & ltv <= 41
replace box = 42 if ltv > 41 & ltv <= 42
replace box = 43 if ltv > 42 & ltv <= 43
replace box = 44 if ltv > 43 & ltv <= 44
replace box = 45 if ltv > 44 & ltv <= 45
replace box = 46 if ltv > 45 & ltv <= 46
replace box = 47 if ltv > 46 & ltv <= 47
replace box = 48 if ltv > 47 & ltv <= 48
replace box = 49 if ltv > 48 & ltv <= 49
replace box = 50 if ltv > 49 & ltv <= 50
replace box = 51 if ltv > 50 & ltv <= 51
replace box = 52 if ltv > 51 & ltv <= 52
replace box = 53 if ltv > 52 & ltv <= 53
replace box = 54 if ltv > 53 & ltv <= 54
replace box = 55 if ltv > 54 & ltv <= 55
replace box = 56 if ltv > 55 & ltv <= 56
replace box = 57 if ltv > 56 & ltv <= 57
replace box = 58 if ltv > 57 & ltv <= 58
replace box = 59 if ltv > 58 & ltv <= 59
replace box = 60 if ltv > 59 & ltv <= 60
replace box = 61 if ltv > 60 & ltv <= 61
replace box = 62 if ltv > 61 & ltv <= 62
replace box = 63 if ltv > 62 & ltv <= 63
replace box = 64 if ltv > 63 & ltv <= 64
replace box = 65 if ltv > 64 & ltv <= 65
replace box = 66 if ltv > 65 & ltv <= 66
replace box = 67 if ltv > 66 & ltv <= 67
replace box = 68 if ltv > 67 & ltv <= 68
replace box = 69 if ltv > 68 & ltv <= 69
replace box = 70 if ltv > 69 & ltv <= 70
replace box = 71 if ltv > 70 & ltv <= 71
replace box = 72 if ltv > 71 & ltv <= 72
replace box = 73 if ltv > 72 & ltv <= 73
replace box = 74 if ltv > 73 & ltv <= 74
replace box = 75 if ltv > 74 & ltv <= 75
replace box = 76 if ltv > 75 & ltv <= 76
replace box = 77 if ltv > 76 & ltv <= 77
replace box = 78 if ltv > 77 & ltv <= 78
replace box = 79 if ltv > 78 & ltv <= 79
replace box = 80 if ltv > 79 & ltv <= 80
replace box = 81 if ltv > 80 & ltv <= 81
replace box = 82 if ltv > 81 & ltv <= 82
replace box = 83 if ltv > 82 & ltv <= 83
replace box = 84 if ltv > 83 & ltv <= 84
replace box = 85 if ltv > 84 & ltv <= 85
replace box = 86 if ltv > 85 & ltv <= 86
replace box = 87 if ltv > 86 & ltv <= 87
replace box = 88 if ltv > 87 & ltv <= 88
replace box = 89 if ltv > 88 & ltv <= 89
replace box = 90 if ltv > 89 & ltv <= 90
replace box = 91 if ltv > 90 & ltv <= 91
replace box = 92 if ltv > 91 & ltv <= 92
replace box = 93 if ltv > 92 & ltv <= 93
replace box = 94 if ltv > 93 & ltv <= 94
replace box = 95 if ltv > 94 & ltv <= 95
replace box = 96 if ltv > 95 & ltv <= 96
replace box = 97 if ltv > 96 & ltv <= 97
replace box = 98 if ltv > 97 & ltv <= 98
replace box = 99 if ltv > 98 & ltv <= 99
replace box = 100 if ltv > 99 & ltv <= 100

g box = 1 if ltv <=10
replace box = 2 if ltv >10 & ltv <= 20
replace box = 3 if ltv > 20 & ltv <= 30
replace box = 4 if ltv > 30& ltv <= 40
replace box = 5 if ltv > 40 & ltv <= 50
replace box = 6 if ltv > 50 & ltv <= 60
replace box = 7 if ltv > 60 & ltv <= 70
replace box = 8 if ltv > 70 & ltv <= 80
replace box = 9 if ltv > 80 & ltv <= 90
replace box = 10 if ltv >90
 
 
  


g box = 1 if ltv <=5
replace box = 2 if ltv >5 & ltv <= 10
replace box = 3 if ltv > 10 & ltv <= 15
replace box = 4 if ltv > 15& ltv <= 20
replace box = 5 if ltv > 20 & ltv <= 25
replace box = 6 if ltv > 25 & ltv <= 30
replace box = 7 if ltv > 30 & ltv <= 35
replace box = 8 if ltv > 35 & ltv <= 40
replace box = 9 if ltv > 40 & ltv <= 45
replace box = 10 if ltv >  45 & ltv <= 50
replace box = 11 if ltv > 50 & ltv <= 55
replace box = 12 if ltv > 55 & ltv <= 60
replace box = 13 if ltv > 60 & ltv <= 65
replace box = 14 if ltv > 65 & ltv <= 70
replace box = 15 if ltv > 70 & ltv <= 75
replace box = 16 if ltv > 75 & ltv <= 80
replace box = 17 if ltv > 80 & ltv <= 85
replace box = 18 if ltv > 85 & ltv <= 90
replace box = 19 if ltv > 90 & ltv <= 95
replace box = 20 if ltv >95




g box = 1 if ltv <=10
replace box = 2 if ltv >10 & ltv <= 20
replace box = 3 if ltv > 20 & ltv <= 30
replace box = 4 if ltv > 30& ltv <= 40
replace box = 5 if ltv > 40 & ltv <= 50
replace box = 6 if ltv > 50 & ltv <= 60
replace box = 7 if ltv > 60 & ltv <= 70
replace box = 8 if ltv > 70 & ltv <= 80
replace box = 9 if ltv > 80 & ltv <= 90
replace box = 10 if ltv >90

 
    

**Loan amount

use U:\work_data\merged_carmen.dta
  keep if _merge==3
rename _merge _mer
  merge m:m  bnk_kod loan_num using "U:\work_data\datagood.dta"
   duplicates drop  loan_num, force
    do "U:\tryNO1\nmatch.do"
gen date=date( dt_bizua_halvaa,"DMY")
 format date %td
gen after=(date>td(1,11,2012))
drop if date<td(25,10,2010)
keep if buyer_cause ==1
nnmatch sum_halvaa_meushar  after  sum_incom  aveAge, tc(att)  m(1) biasadj(bias) keep(H:\u23y\Research\Data\data_mortgage\WorkData\matching\REG2_FOB_attloan.dta) replace

**House Size (rooms, area_net)


use U:\work_data\merged_carmen.dta
gen date=date( dt_bizua_halvaa,"DMY")
 format date %td
gen after=(date>td(1,11,2012))
rename _merge _mer
  merge m:m  bnk_kod loan_num using "U:\work_data\datagood.dta"
  keep if _merge==3
   duplicates drop  loan_num, force
    do "U:\tryNO1\nmatch.do"
drop if date<td(25,10,2010)
drop if area_net==.
keep if buyer_cause ==1
nnmatch  area_net after  sum_incom aveAge, tc(att) m(1) biasadj(bias) keep(H:\u23y\Research\Data\data_mortgage\WorkData\matching\REG2_FOB_areaNETatt.dta) replace

use U:\work_data\merged_carmen.dta
gen date=date( dt_bizua_halvaa,"DMY")
 format date %td
gen after=(date>td(1,11,2012))
 rename _merge _mer
  merge m:m  bnk_kod loan_num using "U:\work_data\datagood.dta"
  keep if _merge==3
   duplicates drop  loan_num, force
    do "U:\tryNO1\nmatch.do"
drop if date<td(25,10,2010)
keep if buyer_cause ==1

nnmatch  rooms after  sum_incom aveAge, tc(att) m(1) biasadj(bias) keep(H:\u23y\Research\Data\data_mortgage\WorkData\matching\REG2_FOB_RoomSatt
.dta) replace

** dist from Tel Aviv


use U:\work_data\merged_carmen.dta
gen date=date( dt_bizua_halvaa,"DMY")
 format date %td
gen after=(date>td(1,11,2012))
rename _merge _mer
  merge m:m  bnk_kod loan_num using "U:\work_data\datagood.dta"
  keep if _merge==3
   duplicates drop  loan_num, force
    do "U:\tryNO1\nmatch.do"
drop if date<td(25,10,2010)
gen x_cord = floor(coordinate_2011 / 100000)
gen y_cord = mod(coordinate_2011, 100000)
local tlv_x = 18055
local tlv_y = 66574
gen dist_from_tlv = ((x_cord - `tlv_x') ^ 2 + (y_cord - `tlv_y') ^ 2) ^ 0.5
replace dist_from_tlv  = dist_from_tlv /100
keep if buyer_cause ==1
nnmatch  dist_from_tlv after  sum_incom aveAge, tc(att) m(1) biasadj(bias) keep(H:\u23y\Research\Data\data_mortgage\WorkData\matching\REG2_FOB_DistF> romTlvAtt.dta) replace


**figure 5

 kdensity  ltv if after==0&group==1, addplot(kdensity  ltv if after==1&group==1) legend(ring(0) pos(2) label(1 "Before") label(2 "After"))
 kdensity  ltv if after==0&group==2, addplot(kdensity  ltv if after==1&group==2) legend(ring(0) pos(2) label(1 "Before") label(2 "After"))
 kdensity  ltv if after==0&group==3, addplot(kdensity  ltv if after==1&group==3) legend(ring(0) pos(2) label(1 "Before") label(2 "After"))

 
 **figure 6
  hist income if after==0, addplot(hist  income if after==1) legend(ring(0) pos(2) label(1 "Before") label(2 "After"))
 hist agextile if after==0, addplot(hist agextile if after==1) legend(ring(0) pos(2) label(1 "Before") label(2 "After"))

 **figure 11
 kdensity  downpayment if year==2011&group==1, addplot(kdensity  downpayment if year==2012&group==1 || kdensity downpayment if year == 2013&group==1)   legend(  label(1 "2011") label(2 "2012") label(2 "2013"))
 
  kdensity  downpayment if year==2011&group==2, addplot(kdensity  downpayment if year==2012&group==2 || kdensity downpayment if year == 2013&group==2)   legend(  label(1 "2011") label(2 "2012") label(2 "2013"))
   kdensity  downpayment if year==2011&group==3, addplot(kdensity  downpayment if year==2012&group==3 || kdensity downpayment if year == 2013&group==3)   legend(  label(1 "2011") label(2 "2012") label(2 "2013"))

gen FTHBtreated=1 if ltv>=75&ltv<=80&group==1
replace FTHBtreated=0 if ltv>70&ltv<75&group==1

 reg price FTHBtreated##after aveAge sum_incom
  reg rooms FTHBtreated##after aveAge sum_incom
  reg area_net FTHBtreated##after aveAge sum_incom
  reg dist_from_tlv FTHBtreated##after aveAge sum_incom
  reg socio_eco_level_08_cbs FTHBtreated##after aveAge sum_incom
    reg interestrate FTHBtreated##after aveAge sum_incom
    reg duration FTHBtreated##after aveAge sum_incom
    reg default FTHBtreated##after aveAge sum_incom

   teffects nnmatch price FTHBtreated##after aveAge sum_incom, vce{iid}
  teffects nnmatch rooms FTHBtreated##after aveAge sum_incom, vce{iid}
  teffects nnmatch area_net FTHBtreated##after aveAge sum_incom, vce{iid}
  teffects nnmatch dist_from_tlv FTHBtreated##after aveAge sum_incom, vce{iid}
  teffects nnmatch socio_eco_level_08_cbs FTHBtreated##after aveAge sum_incom, vce{iid}
  teffects nnmatch interestrate FTHBtreated##after aveAge sum_incom, vce{iid}
  teffects nnmatch duration FTHBtreated##after aveAge sum_incom, vce{iid}
  teffects nnmatch default FTHBtreated##after aveAge sum_incom, vce{iid}

gen upgtreated=1 if ltv>=70&ltv<=75&group==2
replace upgtreated=0 if ltv>65&ltv<70&group==2

 reg price upgtreated##after aveAge sum_incom
  reg rooms upgtreated##after aveAge sum_incom
  reg area_net upgtreated##after aveAge sum_incom
  reg dist_from_tlv upgtreated##after aveAge sum_incom
  reg socio_eco_level_08_cbs upgtreated##after aveAge sum_incom
    reg interestrate upgtreated##after aveAge sum_incom
    reg duration upgtreated##after aveAge sum_incom
    reg default upgtreated##after aveAge sum_incom

   teffects nnmatch price upgtreated##after aveAge sum_incom, vce{iid}
  teffects nnmatch rooms upgtreated##after aveAge sum_incom, vce{iid}
  teffects nnmatch area_net upgtreated##after aveAge sum_incom, vce{iid}
  teffects nnmatch dist_from_tlv upgtreated##after aveAge sum_incom, vce{iid}
  teffects nnmatch socio_eco_level_08_cbs upgtreated##after aveAge sum_incom, vce{iid}
  teffects nnmatch interestrate upgtreated##after aveAge sum_incom, vce{iid}
  teffects nnmatch duration upgtreated##after aveAge sum_incom, vce{iid}
  teffects nnmatch default upgtreated##after aveAge sum_incom, vce{iid}

  
  gen invtreated=1 if ltv>=50&ltv<=55&group==3
replace invtreated=0 if ltv>45&ltv<50&group==3

 reg price invtreated##after aveAge sum_incom
  reg rooms invtreated##after aveAge sum_incom
  reg area_net invtreated##after aveAge sum_incom
  reg dist_from_tlv invtreated##after aveAge sum_incom
  reg socio_eco_level_08_cbs invtreated##after aveAge sum_incom
    reg interestrate invtreated##after aveAge sum_incom
    reg duration invtreated##after aveAge sum_incom
    reg default invtreated##after aveAge sum_incom

   teffects nnmatch price invtreated##after aveAge sum_incom, vce{iid}
  teffects nnmatch rooms invtreated##after aveAge sum_incom, vce{iid}
  teffects nnmatch area_net invtreated##after aveAge sum_incom, vce{iid}
  teffects nnmatch dist_from_tlv invtreated##after aveAge sum_incom, vce{iid}
  teffects nnmatch socio_eco_level_08_cbs invtreated##after aveAge sum_incom, vce{iid}
  teffects nnmatch interestrate invtreated##after aveAge sum_incom, vce{iid}
  teffects nnmatch duration invtreated##after aveAge sum_incom, vce{iid}
  teffects nnmatch default invtreated##after aveAge sum_incom, vce{iid}
